/**
 * 
 */
package com.apache.info.service.plugins;

import java.util.List;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.tools.ClassToolsUtil;

/**
 * @author Administrator
 * 元素管理action插件
 */
public class PubMetadataActionPluginImpl implements PluginConnector {

	private PubMetadataManager pubMetadataManager;
	private PubSortInfoManager pubSortInfoManager;

	/* (non-Javadoc)
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		String method = String.valueOf(param.getParams("method"));
		PubMetadata item = new PubMetadata();
		item = (PubMetadata) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), item);
		ParamsVo vo = new ParamsVo();
		if ("save".equals(method)) {
			saveMetadata(entity, item, vo);
		} else if ("del".equals(method)) {
			delMetadata(entity, item, vo);
		} else if ("list".equals(method)) {
			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			vo.setObj(item);
			if (pageSize == 0 && pageIndex == 0) {
				List<PubMetadata> list = pubMetadataManager.getList(vo);
				entity.setEntity(list);
			} else {
				vo.setParams("pageIndex", pageIndex);
				vo.setParams("pageSize", pageSize);
				vo.setObj(item);
				Page pageList = pubMetadataManager.getPageInfo(vo);
				entity.setEntity(pageList);
			}
		}
		vo = null;
		return entity;
	}

	private void delMetadata(ResultEntity entity, PubMetadata item, ParamsVo vo) {
		if (Validator.isNotNull(item.getMetadataId())) {
			ParamsVo<PubSortInfo> pvo = new ParamsVo<PubSortInfo>();
			pvo.setInfoId(item.getSortId());
			PubSortInfo sort = (PubSortInfo) pubSortInfoManager.getInfoById(pvo);
			if (Validator.isEmpty(sort)) {
				entity.setMessage("请不要恶意操作！");
			} else {
				//if ("1".equals(sort.getSortStatus())) {
				//	entity.setMessage("分类[" + sort.getSortName() + "]已发布启用不能删除元素");
				//}
			}
			if (Validator.isNull(entity.getMessage())) {
				vo.setInfoId(item.getMetadataId());
				boolean mark = pubMetadataManager.deleteInfo(vo);
				if (!mark) {
					entity.setMessage("元素删除失败！");
				} else {
					entity.setMessage("删除成功");
					entity.setEntity("T");
				}
			}
		} else {
			entity.setMessage("请不要恶意操作！");
		}
	}

	private void saveMetadata(ResultEntity entity, PubMetadata item, ParamsVo vo) {
		if (Validator.isNull(item.getMetadataId())) {
			vo.setObj(item);
			String id = pubMetadataManager.saveInfo(vo);
			if (Validator.isNull(id)) {
				entity.setMessage("元素信息添加失败！");
			} else {
				entity.setMessage("保存成功");
				entity.setEntity(id);
			}
		} else {
			vo.setObj(item);
			boolean mark = pubMetadataManager.editInfo(vo);
			if (!mark) {
				entity.setMessage("元素信息修改失败！");
			} else {
				entity.setMessage("修改成功");
				entity.setEntity(item.getMetadataId());
			}
		}
	}

	public void setPubMetadataManager(PubMetadataManager pubMetadataManager) {
		this.pubMetadataManager = pubMetadataManager;
	}

	public void setPubSortInfoManager(PubSortInfoManager pubSortInfoManager) {
		this.pubSortInfoManager = pubSortInfoManager;
	}

}
